import { request } from '@/api/request';
import { API_MEILISEARCH } from '@/constants/api';
import { HttpMethod } from '@/services/types';

/**
 * POST /goods 添加商品
 * @param params
 * @param options
 */
export async function resAddGoods(
  params: API.AddGoodsParams,
  options?: { [key: string]: any }
) {
  return request<R<API.GoodsStruct>>('/goods', {
    method: HttpMethod.POST,
    data: params,
    ...(options || {}),
  });
}

/**
 * PATCH /goods/on/:id 上架和下架
 * @param id
 * @param options
 */
export async function resOnGoods(id: number, options?: { [key: string]: any }) {
  return request<R<API.GoodsStruct>>('/goods/on/' + id, {
    method: HttpMethod.PATCH,
    ...(options || {}),
  });
}

/**
 * PUT /goods/:id 修改商品
 * @param id
 * @param params
 * @param options
 */
export async function resChangeGoods(
  id: number,
  params: API.AddGoodsParams,
  options?: { [key: string]: any }
) {
  return request<R<null>>('/goods/' + id, {
    method: HttpMethod.PUT,
    data: params,
    ...(options || {}),
  });
}

/**
 * POST /indexes/goods/search 全部商品
 * @param params
 * @param options
 */
export async function resGoods(
  params: API.QueryGoodsParams,
  options?: { [key: string]: any }
) {
  return request<API.GoodResult>(
    '/indexes/goods/search',
    {
      method: HttpMethod.POST,
      data: params,
      baseURL: API_MEILISEARCH,
      headers: {
        Authorization: 'Bearer masterKey',
        'Content-Type': 'application/json',
      },
      ...(options || {}),
    },
    false
  );
}

/**
 * Get /goods/my 我出售的商品
 * @param params
 * @param options
 */
export async function resMyGoods(
  params: API.QueryMyGoods,
  options?: { [key: string]: any }
) {
  return request<API.PaginationResult<API.GoodsStruct>>('/goods/my', {
    method: HttpMethod.GET,
    params,
    ...(options || {}),
  });
}

/**
 * Get /goods/users/:id 用户出售的商品
 * @param id
 * @param params
 * @param options
 */
export async function resUsersGoods(
  id: number,
  params: API.QueryMyGoods,
  options?: { [key: string]: any }
) {
  return request<API.PaginationResult<API.GoodsStruct>>('/goods/users/' + id, {
    method: HttpMethod.GET,
    params,
    ...(options || {}),
  });
}

/**
 * DELETE /goods/:id 删除商品
 * @param id
 * @param options
 */
export async function resDeleteGoods(
  id: number,
  options?: { [key: string]: any }
) {
  return request<R<null>>('/goods/' + id, {
    method: HttpMethod.DELETE,
    ...(options || {}),
  });
}

/**
 * GET /admin/goods/:id 商品详情
 * @param id
 * @param options
 */
export async function resGoodsDetail(
  id: number,
  options?: { [key: string]: any }
) {
  return request<API.GoodsDetailStruct>('/goods/' + id, {
    method: HttpMethod.GET,
    ...(options || {}),
  });
}

/*  admin  */

/**
 * GET /admin/goods/:id 后台商品详情
 * @param id
 * @param options
 */
export async function resGoodsDetailAdmin(
  id: number,
  options?: { [key: string]: any }
) {
  return request<R<API.GoodsDetailStruct>>('/admin/goods/' + id, {
    method: HttpMethod.GET,
    ...(options || {}),
  });
}

/**
 * PATCH /admin/goods/recommend/:id 推荐和不推荐
 * @param id
 * @param options
 */
export async function resGoodsRecommendAdmin(
  id: number,
  options?: { [key: string]: any }
) {
  return request<R<null>>('/admin/goods/recommend/' + id, {
    method: HttpMethod.PATCH,
    ...(options || {}),
  });
}

/**
 * Get /goods/my 我出售的商品
 * @param params
 * @param options
 */
export async function resGoodsAdmin(
  params: API.QueryMyGoods,
  options?: { [key: string]: any }
) {
  return request<API.PaginationResult<API.GoodsStruct>>('/admin/goods', {
    method: HttpMethod.GET,
    params,
    ...(options || {}),
  });
}